package com.weds.edwx.web;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;
import com.weds.core.resp.JsonResult;
import com.weds.edpf.core.service.UserBaseService;
import com.weds.edwx.entity.*;
import com.weds.edwx.service.SHAttenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.util.Json;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping(value = "/SHAttenceController")
@Api(value = "锦州石化考勤汇总",description = "锦州石化考勤汇总")
public class SHAttenceController extends BaseController {

    private Logger log = LogManager.getLogger();

    @Autowired
    private SHAttenceService shAttenceService;

    @Autowired
    private UserBaseService userBaseService;

    @Logs
    @ApiOperation(value = "获取部门树",notes = "获取部门树")
    @RequestMapping(value = "/selectDepInfo" , method = RequestMethod.POST)
    public JsonResult selectDepInfo(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        List<SHAttenceEntity> list = shAttenceService.selectDeptUserNums(shAttenceParamEntity);
        SHDepEntity shDepEntity = new SHDepEntity();
        // 人员对象
        //       UserBaseEntity record = new UserBaseEntity();
//        record.setUserDep(depParent);
//        // 获取当前部门下人员
//        List<UserBaseEntity> userList = userBaseService.selectListByEntity(record);

//        // 疑似头像?
//        try {
//            for (UserBaseEntity userBaseEntity : userList) {
//                if (!StringUtils.isBlank(userBaseEntity.getHeadImage())) {
//                    userBaseEntity.setHeadImage(ImageUtils.imgToBase64(userBaseEntity.getHeadImage()));
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        shDepEntity.setUserList(userList);
        shDepEntity.setDepList(list);
        if (list.size() == 0 || list == null){
            if (Integer.parseInt(shAttenceParamEntity.getDepSerial()) == 10000){
                return failMsg("请联系管理员指定部门权限",null);
            }
            return failMsg("暂无下级部门",null);
        }
        if (Integer.parseInt(shAttenceParamEntity.getDepSerial()) == 10000){
            SHAttenceEntity shAttenceEntity = shAttenceService.selectDepParent();
            shDepEntity.setParentDep(shAttenceEntity);
        }
        return succMsgData(shDepEntity);
    }

    @Logs
    @ApiOperation(value = "获取人员", notes = "获取人员")
    @RequestMapping(value = "/selectUserInfo", method = RequestMethod.POST)
    public JsonResult selectUserInfo(@RequestBody BasePageSearch<SHAttenceParamEntity> record){
        setPageHelper(record);
        SHAttenceParamEntity entity = record.getSearch();
        if(entity == null){
            entity = new SHAttenceParamEntity();
        }
        List<SHUserEntity> list = shAttenceService.selectUserInfo(entity);
        return succMsgData(new BaseCommPager<SHUserEntity>(list));
    }

    @Logs
    @ApiOperation(value = "获取部门考勤汇总" , notes = "获取部门考勤汇总")
    @RequestMapping(value = "/selectDepAll" ,method = RequestMethod.POST)
    public JsonResult selectDepAll(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        SHAttenceEntity shAttenceEntity = shAttenceService.selectAttendance(shAttenceParamEntity);
        return succMsgData(shAttenceEntity);
    }

    @Logs
    @ApiOperation(value= "获取人员出勤列表",notes = "获取人员出勤列表")
    @RequestMapping(value = "/selectUserList",method = RequestMethod.POST)
    public JsonResult selectUserList(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        List<SHUserEntity> list = shAttenceService.selectUserList(shAttenceParamEntity);
        //List<SHAttenceEntity> lists = shAttenceService.getDeptUserNums(shAttenceParamEntity);
        for (int i = 0; i < list.size() ; i ++ ){
            SHAttenceParamEntity params = new SHAttenceParamEntity();
            params.setStart(shAttenceParamEntity.getStart());
            params.setEnd(shAttenceParamEntity.getEnd());
            params.setFlag(shAttenceParamEntity.getFlag());
            params.setData(list.get(i).getUserSerial().toString());
            list.get(i).setList(shAttenceService.selectUserLeave(params));
        }
        //SHUserNumsEntity shUserNumsEntity = new SHUserNumsEntity();
        //shUserNumsEntity.setUserList(list);
        //shUserNumsEntity.setUserNumsList(lists);
        return succMsgData(list);
    }

    @Logs
    @ApiOperation(value = "查找人员出勤明细", notes = "查找人员出勤明细")
    @RequestMapping(value = "/selectRecordByUser" , method = RequestMethod.POST)
    public JsonResult selectRecordByUser(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        List<SHUserEntity> list = shAttenceService.selectUserLeave(shAttenceParamEntity);
        return succMsgData(list);
    }

    @Logs
    @ApiOperation(value = "获取部门排班", notes = "获取部门排班")
    @RequestMapping(value = "/selectDepBanci" , method = RequestMethod.POST)
    public JsonResult selectDepBanci(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        if (shAttenceParamEntity.getData().equals("") || shAttenceParamEntity.getData() == null){
            return failMsg();
        }
        List<KtPaiba> list = shAttenceService.selectDepBanci(shAttenceParamEntity);
        List<SHBanciEntity> lists = shAttenceService.selectDepBanciGroup(shAttenceParamEntity);
        for (int i =  0; i < lists.size(); i++){
            List<SHBanciTimeEntity> array = new ArrayList<>();
            if (lists.get(i).getBh().equals("[X]")){
                SHBanciTimeEntity shBanciTimeEntity = new SHBanciTimeEntity();
                shBanciTimeEntity.setSbsj(0);
                shBanciTimeEntity.setXbsj(1439);
                shBanciTimeEntity.setFlag(2);
                shBanciTimeEntity.setTimes("00:00 - 23:59");
                array.add(shBanciTimeEntity);
            } else {
                array = shAttenceService.selectBanciTime(lists.get(i).getBh());
            }

            lists.get(i).setList(array);
        }
        SHResultEntiy shResultEntiy = new SHResultEntiy();
        List<SHUserBnaciEntity> userBanciList = toUserArray(list);
        shResultEntiy.setUserBanciList(userBanciList);
        shResultEntiy.setBanci(lists);
        return succMsgData(shResultEntiy);
    }


    public  List<SHUserBnaciEntity> toUserArray(List<KtPaiba> list){
        List<SHUserBnaciEntity> userBanciList = new ArrayList<>();
        for (int i = 0; i < list.size(); i ++){
            SHUserBnaciEntity shUserBnaciEntity = new SHUserBnaciEntity();
            List<String> arrays = new ArrayList<String>();
            arrays.add(list.get(i).getD1());
            arrays.add(list.get(i).getD2());
            arrays.add(list.get(i).getD3());
            arrays.add(list.get(i).getD4());
            arrays.add(list.get(i).getD5());
            arrays.add(list.get(i).getD6());
            arrays.add(list.get(i).getD7());
            shUserBnaciEntity.setUserName(list.get(i).getUserName());
            shUserBnaciEntity.setBanciList(arrays);
            userBanciList.add(shUserBnaciEntity);
        }
        return userBanciList;
    }

    @Logs
    @ApiOperation(value = "获取人员排班", notes = "获取人员排班")
    @RequestMapping(value = "/selectUserBanci" , method = RequestMethod.POST)
    public JsonResult selectUserBanci(@RequestBody KtPaiba ktPaiba){
        KtPaiba ktPaiba1 = shAttenceService.selectUserPaiba(ktPaiba);
        SHResultEntiy shResultEntiy = new SHResultEntiy();
        // 当前时间没有排班
        if (ktPaiba1 == null){
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 31; i++){
                list.add("");
            }
            shResultEntiy.setPaiban(list);
            List<SHBanciEntity> lists = new ArrayList<>();
            SHBanciEntity shBanciEntity = new SHBanciEntity();
            shBanciEntity.setBh("");
            shBanciEntity.setMc("未排班");
            lists.add(shBanciEntity);
            shResultEntiy.setBanci(lists);
            return succMsgData(shResultEntiy);
        }
        // 当前时间有排班
        List<SHBanciEntity> list = shAttenceService.selectBanci(ktPaiba);
        for(int i= 0; i < list.size() ; i++ ){
            List<SHBanciTimeEntity> array = shAttenceService.selectBanciTime(list.get(i).getBh());
            list.get(i).setList(array);
        }
        shResultEntiy.setPaiban(shAttenceService.toArray(ktPaiba1));
        for (String array :shResultEntiy.getPaiban()){
            if (array.equals("")){
                SHBanciEntity shBanciEntity = new SHBanciEntity();
                shBanciEntity.setBh("");
                shBanciEntity.setMc("未排班");
                list.add(shBanciEntity);
                break;
            }
        }
        // 手动添加休息日班次
        SHBanciEntity shBanciEntity = new SHBanciEntity();
        shBanciEntity.setBh("99999");
        shBanciEntity.setMc("休息日");
        List<SHBanciTimeEntity> xx = new ArrayList<>();
        SHBanciTimeEntity shBanciTimeEntity = new SHBanciTimeEntity();
        shBanciTimeEntity.setFlag(2);
        shBanciTimeEntity.setTimes("00:00 - 23:59");
        xx.add(shBanciTimeEntity);
        shBanciEntity.setList(xx);
        list.add(shBanciEntity);

        shResultEntiy.setBanci(list);
        return succMsgData(shResultEntiy);
    }

    @Logs
    @ApiOperation(value = "获取系统所有班次", notes = "获取系统所有班次")
    @RequestMapping(value = "/selectBanci" , method = RequestMethod.POST)
    public JsonResult selectBanci(){
        List<SHBanciEntity> list = shAttenceService.selectBnaciAll();
        List<String> result = new ArrayList<>();
        for (SHBanciEntity shBanciEntity:list){
            result.add(shBanciEntity.getBh());
        }
        return succMsgData(result);
    }

    @Logs
    @ApiOperation(value = "获取所选部门下所有人员", notes = "获取所选部门下所有人员")
    @RequestMapping(value = "/selectUserNums" , method = RequestMethod.POST)
    public JsonResult selectUserNums(@RequestBody SHAttenceParamEntity shAttenceParamEntity){
        SHAttenceEntity shAttenceEntity = shAttenceService.selectUserNums(shAttenceParamEntity);
        return succMsgData(shAttenceEntity);
    }

}
